﻿namespace LogManager.Gui.UI.Helpers
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Markup;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Resources;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class ImageHelpers
    {
        public static T loadResource<T>(string path)
        {
            T c = default(T);
            StreamResourceInfo sri = Application.GetResourceStream(new Uri(path, UriKind.Relative));

            if (sri.ContentType == "application/xaml+xml")
            {
                c = (T)XamlReader.Load(sri.Stream);

            }
            else if (sri.ContentType.IndexOf("image") >= 0)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = sri.Stream;
                bi.EndInit();
                if (typeof(T) == typeof(ImageSource))
                {
                    c = (T)((object)bi);
                }

                else if (typeof(T) == typeof(Image))
                {
                    Image img = new Image();
                    img.Source = bi;
                    c = (T)((object)img);
                }
            }

            sri.Stream.Close();
            sri.Stream.Dispose();

            return c;
        }
    }
}
